< previous page page_493 next page >

Page 493
    'Make selected text bold face
    objDoc.ActiveWindow.Selection.Font.Bold = True

    'Unselect text
    objDoc.ActiveWindow.Selection.EndOf


End Sub

Private Sub cmdClose_Click()

    'Close word without saving changes
    objWord.Quit False

    'Destroy object reference
    Set objWord = Nothing

End Sub
When you execute the preceding program and click the cmdStart button, a new Word document is created. The code in the Click event procedure uses the Selection object of the document to enter some text and make it boldface.
note.gif In Listing 22.1, a form-level variable for the Word.Application object is used so that it can be referenced from multiple subroutines. square.gif

In Listing 22.1, you created a document object from the application object. You also can just create the document object directly, as in the following example:
Dim objDoc As Word.Document
Set objDoc = New Word.Document
objDoc.ActiveWindow.Selection.TypeText "Hello!"
The preceding code creates a new Word document, although unlike the code in Listing 22.1, it uses an existing instance of Word. In other words, if the user already has Word open, a new document is created in the existing copy of Word; whereas the first example creates a new instance of Word regardless.
When you are finished with document or application objects, you can close a document by using the Close method and close Word by using the Quit method:
objDoc.Close False
objWord.Quit False
The optional parameter False tells Word not to save changes. Omitting this parameter causes Word to prompt the user if a document has not been saved. Depending on your application, you may want to leave Word open for the user to edit a document. In this case, just omit the Close and Quit methods.

 
< previous page page_493 next page >